package com.zhongshengnetwork.rightbe.common;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import com.zhongshengnetwork.rightbe.CustomApplication;

import java.io.File;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

public class NavUtils {

    public static final String PACKEGE_GAODE="com.autonavi.minimap";
    public static final String PACKEGE_BAIDU="com.baidu.BaiduMap";
    public static final String PN_TENCENT_MAP = "com.tencent.map"; // 腾讯地图包名

    public static List<String> initNav(){
        List<String> mapList = new ArrayList<String>();
        if (mapList.size() == 0){
            if (isInstallByread(PACKEGE_GAODE)){
                mapList.add("高德地图");
            }
            if(isInstallByread(PACKEGE_BAIDU)){
                mapList.add("百度地图");
            }
            if (isInstallByread(PN_TENCENT_MAP)){
                mapList.add("腾讯地图");
            }
        }
        return mapList;
    }


    /**
     * 判断是否安装目标应用
     * @param packageName 目标应用安装后的包名
     * @return 是否已安装目标应用
     */
    public static boolean isInstallByread(String packageName) {
        return new File("/data/data/" + packageName).exists();
    }

    /**
     * 城市名 位置名  目的地纬度 目的地经度
     */
    public static void baiduNav(Context context, String city, String position,String lat,String lon) {
        Intent intent = new Intent();
        // 驾车路线规划
        intent.setData(Uri.parse("baidumap://map/direction?"+"origin="+ CustomApplication.latitude+","+CustomApplication.longtitude+"&region="+city+"&destination="+position+"&mode=driving"));//walking
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
        //对局部细节进行了优化。
    }

    //高德导航
    public static void gaodeNav(Context context, String city, String position,String lat,String lon){
        try {
            Intent intent=Intent.parseUri("androidamap://viewMap?sourceApplication=appname&poiname="+position+"&lat="+lat+"&lon="+lon+"&dev=0",0);;
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }


    /**
     * 打开腾讯地图
     * params 参考http://lbs.qq.com/uri_v1/guide-route.html
     *
     * @param context
     * @param slat 起点纬度
     * @param slon 起点经度
     * @param sname 起点名称 可不填（0,0，null）
     * @param dlat 终点纬度
     * @param dlon 终点经度
     * @param dname 终点名称 必填
     * 驾车：type=drive，policy有以下取值
    0：较快捷
    1：无高速
    2：距离
    policy的取值缺省为0
     * &from=" + dqAddress + "&fromcoord=" + dqLatitude + "," + dqLongitude + "
     */
    public static void openTencentMap(Context context, double slat, double slon, String sname, double dlat, double dlon, String dname) {
        String uriString = null;
        StringBuilder builder = new StringBuilder("qqmap://map/routeplan?type=drive&policy=0&referer=zhongshuo");
        if (slat != 0) {
            builder.append("&from=").append(sname)
                    .append("&fromcoord=").append(slat)
                    .append(",")
                    .append(slon);
        }
        builder.append("&to=").append(dname)
                .append("&tocoord=").append(dlat)
                .append(",")
                .append(dlon);
        uriString = builder.toString();
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setPackage(PN_TENCENT_MAP);
        intent.setData(Uri.parse(uriString));
        context.startActivity(intent);
    }

}
